/**
 * @param arr 城市地址三级联动选择器，选择出的code 数组
 * @return 返回解析出 省-市-区/县 的名字 addObj对象
 */
import { Area as AreaData } from '@/utils/amap/areaData'

export function areaCode2AreaName(arr) {
  const [provinceCode, cityCode, countyCode] = arr
  let _province, _city, _county
  const _areaData = AreaData
  if (_areaData.length > 0) {
    for (const k in _areaData) {
      if (_areaData[k].value === provinceCode) {
        _province = _areaData[k].label
        const _areaData2 = _areaData[k].children
        for (const k2 in _areaData2) {
          if (_areaData2[k2].value === cityCode) {
            _city = _areaData2[k2].label
            const _areaData3 = _areaData2[k2].children
            for (const k3 in _areaData3) {
              if (_areaData3[k3].value === countyCode) {
                _county = _areaData3[k3].label
                break
              }
            }
          }
        }
      }
    }
  }
  return {
    provinceName: _province, cityName: _city, countyName: _county
  }
}

/**
 * @param arr 城市地址三级联动选择器，选择出的code 数组
 * @return 返回解析出 省-市-区/县 的code编码 addObj对象
 */
export function areaName2AreaCode(arr) {
  // console.log('areaName2AreaCode()-地址名转code', arr)
  const [provinceName, cityName, countyName] = arr
  let _province, _city, _county
  const _areaData = AreaData
  let addObj = {}
  if (_areaData.length > 0) {
    for (const k in _areaData) {
      if (_areaData[k].label === provinceName) {
        _province = _areaData[k].value
        // console.log('areaName2AreaCode()-地址名转code-_province', _province)
        // 重庆市 500000   ,北京市 110000 ,  上海市 310000,  天津市 120000, 香港特别行政区  810000,  澳门特别行政区 820000
        if (_province === '500000' || _province === '110000' || _province === '310000' || _province === '120000' || _province === '810000' || _province === '820000') {
          const _areaData2 = _areaData[k].children
          let now_cityName = provinceName
          if (provinceName === '香港特别行政区' || provinceName === '澳门特别行政区') {
            now_cityName = provinceName.replace('特别行政区', '')
          }
          for (const k2 in _areaData2) {
            if (_areaData2[k2].label === now_cityName) {
              _city = _areaData2[k2].value
              const _areaData3 = _areaData2[k2].children
              for (const k3 in _areaData3) {
                // 兼容处理 countyName输入的值中有 XX新区 及 XX区 这种值不确定 不匹配问题
                let countCountyName = ''
                if (countyName.indexOf('新区') !== -1) {
                  countCountyName = countyName.substring(0, countyName.indexOf('新区'))
                } else {
                  countCountyName = countyName
                }
                if (_areaData3[k3].label.indexOf(countCountyName) !== -1) {
                  _county = _areaData3[k3].value
                  break
                }
              }
            }
          }
        } else {
          const _areaData2 = _areaData[k].children
          for (const k2 in _areaData2) {
            if (_areaData2[k2].label === cityName) {
              _city = _areaData2[k2].value
              const _areaData3 = _areaData2[k2].children
              for (const k3 in _areaData3) {
                // 兼容处理 countyName输入的值中有 XX新区 及 XX区 这种值不确定 不匹配问题
                let countCountyName = ''
                if (countyName.indexOf('新区') !== -1) {
                  countCountyName = countyName.substring(0, countyName.indexOf('新区'))
                } else {
                  countCountyName = countyName
                }
                if (_areaData3[k3].label.indexOf(countCountyName) !== -1) {
                  _county = _areaData3[k3].value
                  break
                }
              }
            }
          }
        }
      }
    }
  }
  addObj = {
    provinceCode: _province, cityCode: _city, countyCode: _county
  }
  return addObj
}

// String: null => ''
export function stringNullToValue(string) {
  let nowStr = ''
  if (string) {
    nowStr = string
  }
  return nowStr
}
